<?php

namespace app\admin\controller;

class Admingroup extends Base
{
    //列表 
    public function lists()
    {
        if (is_post()) {
            $d = $this->post;
            $map = [];
            if ($d['keyword'] != '') {
                $map[] = ['and', 'title', 'like', "%{$d['keyword']}%"];
            }
            $w = where($map);
            $rs = $this->db->query("select *  from admin_group  $w[0] order by id desc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                $rule = [
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0], $rule);
                suc($data, $rs[1]);
            } else {
                suc([], 0);
            }
            $this->fetch();
        }
        return $this->fetch();
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
            ];
            $msg = [
                'title.require' => '部门名称不能为空',
            ];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            $id = $this->db->insert('admin_group',$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $this->fetch();
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
              'title'=>'require',
            ];
            $msg = [
              'title.require' => '部门名称不能为空',
            ];
            check($rule,$msg,$d);
            $d['update_time'] = time();
            $d['content'] = implode(",",array_unique(array_filter($d['content'])));
            $id = $this->db->update('admin_group',$d);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }
        $id = $this->get['id'];
        $info = $this->db->query("select * from admin_group where id=?",[$id],1);
        $menu = c('adminmenu');
        $arr_id = admin_menu_arr($menu,0);
        $tree   = admin_menu_tree($arr_id,0);
        $content = explode(",",$info['content']);
        $right_url = $this->db->query("select * from admin_power ");
        return $this->fetch(['info'=>$info,'tree'=>$tree, "content"=>$content,'right_url'=>$right_url]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('admin_group',$d['id']);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }
    #批量删除
    function delall(){
        if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('admin_group',$id);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    //详情
    public function detail($id)
    {
        return $this->fetch();
    }
}
